#!/bin/sh -x

# POST-COMMIT HOOK
#
# The post-commit hook is invoked after a commit.  Subversion runs
# this hook by invoking a program (script, executable, binary, etc.)
# named 'post-commit' (for which this file is a template) with the 
# following ordered arguments:
#
#   [1] REPOS-PATH   (the path to this repository)
#   [2] REV          (the number of the revision just committed)
#
# The default working directory for the invocation is undefined, so
# the program should set one explicitly if it cares.
#
# Because the commit has already completed and cannot be undone,
# the exit code of the hook program is ignored.  The hook program
# can use the 'svnlook' utility to help it examine the
# newly-committed tree.
#
# On a Unix system, the normal procedure is to have 'post-commit'
# invoke other programs to do the real work, though it may do the
# work itself too.
#
# Note that 'post-commit' must be executable by the user(s) who will
# invoke it (typically the user httpd runs as), and that user must
# have filesystem-level permission to access the repository.
#
# On a Windows system, you should name the hook program
# 'post-commit.bat' or 'post-commit.exe',
# but the basic idea is the same.
# 
# The hook program typically does not inherit the environment of
# its parent process.  For example, a common problem is for the
# PATH environment variable to not be set to its usual value, so
# that subprograms fail to launch unless invoked via absolute path.
# If you're having unexpected problems with a hook program, the
# culprit may be unusual (or missing) environment variables.
# 
# Here is an example hook script, for a Unix /bin/sh interpreter.
# For more examples and pre-written hooks, see those in
# the Subversion repository at
# http://svn.apache.org/repos/asf/subversion/trunk/tools/hook-scripts/ and
# http://svn.apache.org/repos/asf/subversion/trunk/contrib/hook-scripts/

#/bin/echo "PATH value: ${PATH}" > /tmp/post-commit.log;


#######################################################################
####### EZDeploy svn post-commit autodeploy hook #############
export LANG=en_US.UTF-8

OLD_PATH=${PATH};
if [ -z "${PATH}" ];
then
    export PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
else
    export PATH=$PATH:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
fi

DEPLOYURL="http://192.168.8.104:8080/neatlogic/anonymous/api/rest/deploy/ci/svn/event/callback/dev";
BASIC_AUTH_USER=''
BASIC_AUTH_PWD=''
LOG_FILE="/tmp/post-commit.log"

REPOS="$1"
REV="$2"

event='post-commit'
dirsChanged=''
author=`svnlook author $REPOS -r $REV`
date=`svnlook date $REPOS -r $REV`
log=`svnlook log $REPOS -r $REV`
added=''
modified=''
deleted=''

# 如果提交信息中没有 --autodeploy，不执行后续持续集成动作
#if [ -z `svnlook log $REPOS -r $REV | grep -o "\--autodeploy"` ]; then
  # exit 0
#fi

OLD_IFS=${IFS}
IFS=$'\n'

for dir in `svnlook dirs-changed $REPOS -r $REV`
do
    dirsChanged="${dirsChanged}${dir},"
done
dirsChanged=${dirsChanged%,}

for line in `svnlook changed $REPOS -r $REV`
do
    content=${line#* }
    action=${line%% *}
    if [ "$action" = "A" ];then
        added="${added}${content},"
    elif [ "$action" = "U" ] || [ "$action" = "UU" ] || [ "$action" = "_U" ];then
        modified="${modified}${content},"
    elif [ "$action" = "D" ];then
        deleted="${deleted}${content},"
    fi
done

# 获取本机IP，如果是多网卡多IP，则ip之间以","分隔
ip=''
ipAddr=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
ipArray=(`echo $ipAddr | tr '\n' ' '`)
ipNum=${#ipArray[@]}
if [ $ipNum -eq 1 ]
then
    ip=${ipArray[0]}
elif [ $ipNum -gt 1 ]
then
    for i in ${ipArray[@]}
    do
        if [ -n "$ip" ]
        then
            ip=''$ip','$i'';
        else
            ip=$i;
        fi
    done
fi

# 格式化数据
added=${added%,}
modified=${modified%,}
deleted=${deleted%,}
date=${date% +*}

data="{event:'$event',repo:'${REPOS}',dirsChanged:'${dirsChanged}',revision:'${REV}',author:'${author}',date:'${date}',message:'${log}',added:'${added}',modified:'${modified}',deleted:'${deleted}',ip:'$ip'}"

#echo "old path: $OLD_PATH" > /tmp/post-commit.log
#echo "new path: $PATH" >> /tmp/post-commit.log
echo "">>${LOG_FILE}
echo "==========================`date '+%Y-%m-%d %H:%M:%S'`==============================" >> ${LOG_FILE}
echo "deploy RESTFul url is $DEPLOYURL" >> ${LOG_FILE}
echo "svn post-commit upload data is: $data" >> ${LOG_FILE}
echo "ci result:" >> ${LOG_FILE}
curl -s  --basic -u ${BASIC_AUTH_USER}:${BASIC_AUTH_PWD} -X POST -H "Content-Type:application/json" --data "${data}" "${DEPLOYURL}" 2>&1|tee -a ${LOG_FILE};
echo "">>${LOG_FILE}

IFS=${OLD_IFS}
PATH=${OLD_PATH}

